/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Aug 21, 2009 10:27:53 PM $
 *
 * Author: Eric Yang
 * Date  : Aug 21, 2009 10:27:53 PM
 *
 */
package com.gooex.domain.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gooex.domain.common.Region;

/**
 * 商品分类级别 <br>
 * 商品-->子类别-->子类别 <br>
 * <br>
 * 某一个产品属于某一个(叶子)类别,产品可包含一系列参数。<br>
 * eg:<br>
 * 产品:NOKIA E71<br>
 * 类别:手机-->nokia<br>
 * 参数:<br>
 * 颜色:黑色,银色<br>
 * 产地:德国,中国<br>
 * 
 * @author Eric Yang
 * @version 1.0
 */
public class Category implements Serializable {

	/** 类别编号(设定为6位,如:CD0001,EF5602,GT7607) */
	private String id;

	/** category'name */
	private String name;

	/** parent category */
	private Category parent;

	private long count = 0;

	/** child categories */
	private List<Category> children = new ArrayList<Category>();

	private Map<String, Long> regionCountMap = new HashMap<String, Long>();

	/* ===========functions============================== */
	/**
	 * 获取类别链式名称<br>
	 * <br>
	 * 如:品牌 - 类型 - 型号 - 颜色
	 */
	public String getChainName() {
		if (parent == null)
			return name;
		else
			return parent.getChainName() + " - " + name;
	}

	public String[] getChainNames() {
		return getChainName().split(" - ");
	}

	public String getChainId() {
		if (parent == null)
			return id;
		else
			return parent.getChainId() + "," + id;
	}

	public String[] getChainIds() {
		return getChainId().split(",");
	}

	/** category level */
	public int getLevel() {
		if (parent == null)
			return 1;
		else
			return parent.getLevel() + 1;
	}

	/** whether top header level */
	public boolean isHeader() {
		return getLevel() == 1;
	}

	/** whether category leaf */
	public boolean isLeaf() {
		return children == null || children.size() == 0;
	}

	/** add child categroy */
	public void addChild(Category child) {
		if (child != null && !children.contains(child)) {
			children.add(child);
			child.setParent(this);
		}
	}

	/** remove child category */
	public void removeChild(Category child) {
		if (child != null && children.contains(child)) {
			children.remove(child);
			child.setParent(null);
		}
	}

	/** remove category parent relation */
	public void removeParent() {
		if (parent != null) {
			this.parent.removeChild(this);
		}
	}

	/** change the category parent */
	public void changeParent(Category parent) {
		if (parent != null) {
			parent.addChild(this);
		} else {
			if (this.parent != null) {
				this.parent.removeChild(this);
			}
			this.parent = parent;
		}
	}

	/* 是否是其他类别 */
	public boolean isOther() {
		return id != null && id.length() >= 2 && "00".equals(id.substring(id.length() - 2));
	}

	public void increaseCount(String region) {
		if (region == null)
			return;
		this.count++;
		Long l = regionCountMap.get(region);
		regionCountMap.put(region, l == null ? new Long(1) : ++l);
	}

	public void decreaseCount(String region) {
		if (region == null)
			return;
		this.count--;
		if (this.count < 0)
			this.count = 0;
		Long l = regionCountMap.get(region);
		Long ll = l == null ? new Long(0) : --l;
		regionCountMap.put(region, ll < 0 ? 0 : ll);
	}

	public long getRegionCount(String region) {
		if (region == null || Region.GLOBAL_REGION.equals(region))
			return this.count;
		Long l = regionCountMap.get(region);
		return l == null ? 0 : l;
	}

	/* ===========setter/getter============================== */
	public List<Category> getChildren() {
		return children;
	}

	public void setChildren(List<Category> children) {
		this.children = children;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Category getParent() {
		return parent;
	}

	public void setParent(Category parent) {
		this.parent = parent;
	}

	public long getCount() {
		return count;
	}

	public void setCount(long count) {
		this.count = count;
	}

	public Map<String, Long> getRegionCountMap() {
		return regionCountMap;
	}

	public void setRegionCountMap(Map<String, Long> regionCountMap) {
		this.regionCountMap = regionCountMap;
	}

}
